<!--
*  2021-03-15
*  库存出库
-->
<template>
  <div id="app" class="weaving-package">
    <!-- 头部导航 -->
    <van-nav-bar
      :title="title"
      ref="header"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    >
      <template #right>
        <span @click="onClickStart"><van-icon name="plus" size="20" /></span>
      </template>
    </van-nav-bar>
    <van-cell-group>
      <van-cell
        :title="'编号：' + no"
        :label="colorName + standardName"
        :value="'库存：' + stock"
      >
      </van-cell>
      <!-- <van-cell title="出库批次" :value="lotNum"></van-cell> -->
      <van-cell title="出库批次" is-link @click="showPicker = true">
        <template>
          <van-field v-model="lotNum" placeholder="请选择" readonly></van-field>
        </template>
      </van-cell>
      <van-cell title="出库重量(KG)">
        <template>
          <van-field
            v-model="weight"
            type="tel"
            placeholder="输入标重"
            clearable
          ></van-field>
        </template>
      </van-cell>
    </van-cell-group>
    <van-cell-group class="weaving-package-btn">
      <van-button block type="info" class="bgColor" @click="onConfirmSubmit"
        >确认出库</van-button
      >
    </van-cell-group>

    <van-popup v-model="showPicker" position="bottom">
      <van-picker
        :title="no"
        show-toolbar
        value-key="label"
        :columns="columns"
        @confirm="onConfirm"
        @cancel="onCancel"
      />
    </van-popup>
  </div>
</template>

<script>
import { comeBack } from "@/utils/index";
import { setYarnList, getYarnList, removeYarnList } from "@/utils/auth";
import {
  getYarnLotNumList,
  getYarnStoreOutCheck,
  getYarnPlanInfo,
  getYarnStoreByStoreId,
} from "@/api/yarn";

export default {
  name: "weavingPackage",
  data() {
    return {
      title: "纱线库存出库",
      weight: "",
      standardId: "",
      no: "",
      name: "",
      colorName: "",
      standardName: "",
      stock: "",
      lotNum: "",
      columns: [],
      showPicker: false,
      totalWeight: 0,
    };
  },
  created() {
    const that = this;
    if (process.env.NODE_ENV === "production") {
      this.$nextTick(() => {
        that.initFn(that);
      });
    } else {
      window.apiready = function () {
        that.initFn(that);
      };
    }
  },
  methods: {
    //初始化
    initFn(that) {
      removeYarnList();
      window.api.addEventListener({
        name:'viewdisappear'
      }, function() {
        window.api.closeWin();
      });
    },
    // 返回
    onClickLeft() {
      removeYarnList();
      comeBack();
    },
    // 扫码
    onClickStart() {
      const that = this;
      const zyHmsScan = api.require('zyHmsScan');
      zyHmsScan.openDefaultView({}, ret => {
        if(ret.status) {
          let id = ret.result;
          let index = id.indexOf("taskId=");
          that.standardId = "";
          that.no = "";
          that.name = "";
          that.colorName = "";
          that.standardName = "";
          that.stock = "";
          that.lotNum = "";
          that.totalWeight = "";
          if (index != -1) {
            let taskId = id.slice(index + 7, id.length);
            getYarnPlanInfo(
                {
                  taskId: taskId,
                },
                (res) => {
                  let yarnList = [];
                  if (getYarnList()) {
                    yarnList = getYarnList();
                  }
                  if (res && res.length) {
                    res.forEach((item) => {
                      yarnList.push(item.yarnId);
                    });

                    let list = [...new Set(yarnList)];
                    setYarnList(list);
                    that.$toast({
                      type: "success",
                      message: "扫码成功，继续出库",
                      duration: 1500,
                      onClose: () => {
                        that.onClickStart();
                      },
                    });
                  }
                }
            );
          } else {
            if (id.indexOf(",") != -1) {
              if (!that.$isJSON(id) || id.indexOf(",") == -1) {
                that.$toast.fail("二位码异常");
                return false;
              }
              let ids = eval(id);
              if (ids.toString().indexOf(",") == -1) {
                that.$toast.fail("二位码异常");
                return false;
              }
              id = ids.split(",")[0].toString();
            }
            let yarnList = getYarnList() || [];
            if (yarnList.length === 0) {
              that.$toast.fail("请先扫生产码");
            } else {
              getYarnStoreByStoreId(id, (res) => {
                let index = yarnList.findIndex((item) => item === res.yarnId);
                if (index === -1) {
                  that.$toast.fail("非所需纱线,请扫相应生产码");
                  return false;
                } else {
                  that.standardId = res.yarnId;
                  that.no = res.no;
                  that.name = res.name;
                  that.colorName = res.colorName;
                  that.standardName = res.standard;
                  that.stock = res.stock;
                  that.lotNum = res.lotNum;
                  that.totalWeight = res.stock;
                  that.onGetLotNumByyarnId();
                }
              });
            }
          }
        }
      });
    },
    // 根据规格id 获取批次
    onGetLotNumByyarnId() {
      getYarnLotNumList(
        {
          standardId: this.standardId,
        },
        (res) => {
          res.forEach((item) => {
            this.columns.push({
              label:
                "批次：" +
                item.lotNum +
                "&nbsp&nbsp;&nbsp&nbsp;&nbsp&nbsp;库存：" +
                item.stock,
              value: item.lotNum,
              stock: item.stock,
            });
          });
        }
      );
    },
    // 选择批次
    onConfirm(val) {
      this.lotNum = val.value;
      this.totalWeight = val.stock;
      this.showPicker = false;
    },
    onCancel() {
      this.showPicker = false;
    },
    // 出库
    onConfirmSubmit() {
      const that = this;
      if (that.weight > that.totalWeight) {
        that.$toast.fail("出库重量大于批次库存");
        return;
      }
      getYarnStoreOutCheck(
        {
          standardId: this.standardId,
          weight: this.weight,
          lotNum: this.lotNum,
        },
        () => {
          that.$toast({
            type: "success",
            message: "出库成功",
            duration: 1000,
            onClose: () => {
              that.standardId = "";
              that.no = "";
              that.name = "";
              that.colorName = "";
              that.standardName = "";
              that.stock = "";
              that.lotNum = "";
              that.totalWeight = "";
            },
          });
        }
      );
    },
  },
};
</script>

<style lang="scss">
.weaving-package {
  display: flex;
  flex-direction: column;
  .van-cell {
    .van-cell__value {
      flex: 0 0 30%;
      .van-field {
        padding: 0;
        display: flex;
        .van-cell__value {
          flex: 1;
        }
        .van-field__control {
          text-align: right;
        }
      }
    }
  }
  .weaving-package-btn {
    padding: 16px;
    &.van-hairline--top-bottom::after,
    .van-hairline-unset--top-bottom::after {
      border-width: 0;
    }
  }
}
</style>
